package hdp.servlet;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;

import com.google.gson.Gson;

import bean.ResponseData;
import dao.ActivityDao;
import dao.FoodDao;
import dao.impl.ActivityImpl;
import dao.impl.BaseDao;
import dao.impl.FoodDaoImpl;


@WebServlet("/detail")
public class detail extends HttpServlet {
	FoodDao fdao= new FoodDaoImpl();
	ActivityDao adao=new ActivityImpl();
	Gson gson=new Gson();
	ResponseData data=new ResponseData();
	HttpServletRequest req;
	HttpServletResponse resp;
	
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		this.req=req ;
		this.resp =resp ;
		req.setCharacterEncoding("utf-8");
		resp.setCharacterEncoding("utf-8");
		resp.setHeader("Content-Type", "text/html;charset=utf-8");
		
		try {
			if ("selectF".equals(req.getParameter("type"))) {
				Integer foodid=getInt("foodid");
//				String zhufu=getString("zhufu");12
				String sql="SELECT p.*,a.* FROM \r\n" + 
						"(SELECT foodid as id,hosterid,foodtitle as title,foodprice as price,foodjoinernum as num,"
						+ "foodtime as time,foodaddress as address,foodintroduction as introduction,interest "
						+ "FROM food WHERE NOW()<foodtime) a,photo  p WHERE a.id=p.suozaiid AND p.phototype=1 "
						+ "AND a.id=? and zhufu=1";
				Map<String, Object> queryList=BaseDao.query.query(sql, new MapHandler(),foodid);
				resp.getWriter().write(gson.toJson(queryList));
				
			}
			else if ("selectH".equals(req.getParameter("type"))) {
				Integer activityid=getInt("activityid");
//				String zhufu=getString("zhufu");
				String sql="SELECT p.*,a.* FROM (SELECT activityid as id,hosterid,activitytitle as title,activityprice as price,"
						+ "activitynum as num,activitytime as time,activityadress as address,activityplay,"
						+ "activeintroduction as introduction,activityentertainment,activetype,\r\n" + 
						"interest FROM activity WHERE NOW()<activitytime) a,photo p \r\n" + 
						"WHERE a.id=p.suozaiid AND p.phototype=2 AND a.id=? and zhufu=1";
				Map<String, Object> queryList=BaseDao.query.query(sql, new MapHandler(),activityid);
				resp.getWriter().write(gson.toJson(queryList));
				
			}else if ("selectF2".equals(req.getParameter("type"))) {
				Integer foodid=getInt("foodid");
				String sql="SELECT p.c FROM \r\n" + 
						"(SELECT foodid as id,hosterid,foodtitle as title,foodprice as price,foodjoinernum as num,"
						+ "foodtime as time,foodaddress as address,foodintroduction as introduction,interest "
						+ "FROM food WHERE NOW()<foodtime) a,photo  p WHERE a.id=p.suozaiid AND p.phototype=1 "
						+ "AND a.id=? and zhufu=2";
				Map<String, Object> queryList=BaseDao.query.query(sql, new MapHandler(),foodid);
				resp.getWriter().write(gson.toJson(queryList));
				
			}
			else if ("selectH2".equals(req.getParameter("type"))) {
				Integer activityid=getInt("activityid");
//				String zhufu=getString("zhufu");
				String sql="SELECT p.c FROM "
						+ "(SELECT activityid as id,hosterid,activitytitle as title,activityprice as price,"
						+ "activitynum as num,activitytime as time,activityadress as address,activityplay,"
						+ "activeintroduction as introduction,activityentertainment,activetype,\r\n" + 
						"interest FROM activity WHERE NOW()<activitytime) a,photo p \r\n" + 
						"WHERE a.id=p.suozaiid AND p.phototype=2 AND a.id=? and zhufu=2";
				Map<String, Object> queryList=BaseDao.query.query(sql, new MapHandler(),activityid);
				resp.getWriter().write(gson.toJson(queryList));
				
			}
			else if ("selectFimg".equals(req.getParameter("type"))) {
				Integer foodid=getInt("foodid");
				String sql="SELECT * FROM (SELECT foodid as id,hosterid,foodtitle as title,foodprice as price,"
						+ "foodjoinernum as num,foodtime as time,foodaddress as address,foodintroduction as introduction,"
						+ "interest FROM food WHERE NOW()<foodtime) f,`user` u "
						+ "WHERE f.hosterid=u.userid AND f.id=?";
				Map<String, Object> queryList=BaseDao.query.query(sql, new MapHandler(),foodid);
				resp.getWriter().write(gson.toJson(queryList));
				
			}
			else if ("selectHimg".equals(req.getParameter("type"))) {
				Integer activityid=getInt("activityid");
				String sql="SELECT * FROM (SELECT activityid as id,hosterid,activitytitle as title,activityprice as price,"
						+ "activitynum as num,activitytime as time,activityadress as address,activityplay,"
						+ "activeintroduction as introduction,activityentertainment,activetype,\r\n" + 
						"interest FROM activity WHERE NOW()<activitytime) a,`user` u "
						+ "WHERE a.hosterid=u.userid AND a.id=?";
				Map<String, Object> queryList=BaseDao.query.query(sql, new MapHandler(),activityid);
				resp.getWriter().write(gson.toJson(queryList));
				
			}
			else if ("selectimg".equals(req.getParameter("type"))) {
				Integer userid=getInt("userid");
				String sql="select userid,username,a FROM `user`WHERE userid=?";
				List<Map<String, Object>> queryList=BaseDao.query.query(sql, new MapListHandler(),userid);
				resp.getWriter().write(gson.toJson(queryList));
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public boolean isNull(String s) {
		return req.getParameter(s)==null;
	}
	
	public Integer getInt(String s) {
		if (!isNull(s)) {
			return Integer.parseInt(req.getParameter(s));
			
		}
		return null;
	}
	
	public String getString(String s) {
		if (!isNull(s)) {
			return req.getParameter(s);
			
		}
		return null;
	}

}
